﻿var patientVM = {
    Patient: ko.observableArray([]),
    Visits: ko.observableArray([]),
    VisitsPage: ko.observable(0),
    VisitsPageCount: ko.observable(0),
    VisitsPageNavHandling: function (indexController) {
        var self = this;
        if (self.VisitsPage() > 1) {
            indexController.enableVisitPreviousGroup(true);
        }
        if (self.VisitsPage() < self.VisitsPageCount()) {

            indexController.enableVisitNextGroup(true);
        }
    },
    sortVisitsDESC: function () {
        this.Visits.sort(function (left, right) { return left.VisitID == right.VisitID ? 0 : (left.VisitID < right.VisitID ? 1 : -1) });
    },
    loadPatientInfo: function (indexController) {
        var self = this;
        indexController.message.processing("processing");
        indexController.enableListVisit(false);
        indexController.enableVisitPreviousGroup(false);
        indexController.enableVisitNextGroup(false);
        self.Patient.removeAll();
        self.Visits.removeAll();
        self.VisitsPage(0);
        self.VisitsPageCount(0);
        $.ajax({
            type: 'post',
            url: home + 'Patient/GetPatientInfo',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                self.Patient(data[0]);
                self.Visits(data[1]);
                //self.sortVisitsDESC();
                self.VisitsPage(data[2].PageNumber);
                self.VisitsPageCount(data[2].PageCount);
                self.VisitsPageNavHandling(indexController);
                indexController.enableListVisit(true);
                indexController.message.processComplete("process_complete");
            },
            error: function (err) {
                if (err.status == 200) {
                    indexController.message.processComplete(err.responseText);
                }
                else {
                    indexController.message.processComplete("process_error");
                }
            }
        });
    },
    loadVisitsPageRe: null,
    loadVisitsPage: function (indexController, pageNumber) {
        var self = this;
        //stop handling
        indexController.enableListVisit(false);
        indexController.enableVisitPreviousGroup(false);
        indexController.enableVisitNextGroup(false);

        indexController.message.processing("processing");
        //remove old data
        self.Visits.removeAll();
        //Ajax Call Get Patient Info
        self.loadVisitsPageRe = $.ajax({
            type: 'post',
            url: home + 'Patient/GetVisitsPage',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ PatientID: self.Patient()[0].ID, RowsPerPage: 10, PageNumber: pageNumber }),
            success: function (data) {
                self.Visits(data[0]);
                self.VisitsPage(data[1].PageNumber);
                self.VisitsPageCount(data[1].PageCount);
                self.VisitsPageNavHandling(indexController);
                indexController.enableListVisit(true);
                indexController.message.processComplete("process_complete");
            },
            error: function (err) {
                if (err.status == 200) {//if server return string-success but no matched data
                    messageController.processComplete(err.responseText);
                }
                else {
                    messageController.processComplete("process_error");
                }
            },
            complete: function (xhr, textStatus) {
            }
        });
    }
};
var VisitsPageInfo = ko.computed(function () {
    return "Page: " + patientVM.VisitsPage() + "/" + patientVM.VisitsPageCount();
});